﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Uber.Messages;

using Uber.HabboHotel.Rooms;
using Uber.HabboHotel.Pathfinding;
using Uber.HabboHotel.Items;

namespace Uber.HabboHotel.RoomBots
{
    class PetBot : BotAI
    {
        private int SpeechTimer;
        private int ActionTimer;
        private int EnergyTimer;

        // TO-DO: This needs cleaning up BADLY, If anyone wants to attempt cleaning up my mess, go for it (Y) (Half done - Shorty)

        public PetBot(int VirtualId)
        {
            this.SpeechTimer = new Random((VirtualId ^ 2) + DateTime.Now.Millisecond).Next(10, 60);
            this.ActionTimer = new Random((VirtualId ^ 2) + DateTime.Now.Millisecond).Next(10, 30 + VirtualId);
            this.EnergyTimer = new Random((VirtualId ^ 2) + DateTime.Now.Millisecond).Next(10, 60);
        }

        private Coord GetBasketCoord()
        {
            for (int i = 0; i < GetRoom().Items.Count; i++)
            {
                RoomItem Item = GetRoom().Items[i]; if (Item == null) { continue; }

                if (Item.GetBaseItem().Name == "nest")
                {
                    return Item.Coordinate;
                }
            }

            return new Coord(-1,-1);
        }

        private void RemovePetStatus()
        {
            RoomUser Pet = GetRoomUser();

            if (Pet == null)
            {
                return;
            }

            Pet.AddStatus("gst thr", "");

            Pet.Statusses = new Dictionary<string, string>();
            Pet.UpdateNeeded = true;
        }

        public override void OnSelfEnterRoom()
        {

        }

        public override void OnSelfLeaveRoom(bool Kicked) { }

        public override void OnUserEnterRoom(Rooms.RoomUser User)
        {
            if (User.GetClient().GetHabbo().Username.ToLower() == GetRoomUser().PetData.OwnerName.ToLower())
            {
                GetRoomUser().Chat(null, "*Hello master!*", false);
            }
        }

        public override void OnUserLeaveRoom(GameClients.GameClient Client) { }

        #region Commands
        public override void OnUserSay(Rooms.RoomUser User, string Message)
        {
            RoomUser Pet = GetRoomUser();

            if (Pet == null || Message.Length <= 0)
            {
                return;
            }

            if (Message.ToLower().Equals(Pet.PetData.Name.ToLower()))
            {
                Pet.SetRot(Rotation.Calculate(Pet.X, Pet.Y, User.X, User.Y));
                return;
            }

            if (Message.ToLower().StartsWith(Pet.PetData.Name.ToLower() + " ") && GetRoom().CheckRights(User.GetClient(),true))
            {
                string Command = Message.Substring(Pet.PetData.Name.ToLower().Length + 1);

                int r = UberEnvironment.GetRandomNumber(1, 8); // Made Random

                if (Pet.PetData.Energy > 10 && r < 6 || Pet.PetData.Level > 15)
                {
                    RemovePetStatus();
                    int Level = Pet.PetData.Level;

                    if (Command == "free" && Level >= 1)
                    {
                        Pet.PetData.AddExpirience(10);

                        int randomX = UberEnvironment.GetRandomNumber(0, GetRoom().Model.MapSizeX);
                        int randomY = UberEnvironment.GetRandomNumber(0, GetRoom().Model.MapSizeY);

                        if (randomX <= GetRoom().Model.MapSizeX && randomY <= GetRoom().Model.MapSizeY)
                        {
                            GetRoomUser().MoveTo(randomX, randomY);
                        }
                    }
                    else if (Command == "sit" && Level >= 2)
                    {
                        Pet.AddStatus("sit", "");
                        Pet.PetData.AddExpirience(10);

                        EnergyTimer = 10;
                        ActionTimer = 10;
                    }
                    else if (Command == "lay" && Level >= 3)
                    {
                        Pet.AddStatus("lay", "");
                        Pet.PetData.AddExpirience(10);

                        EnergyTimer = 10;
                        ActionTimer = 10;
                    }
                    else if (Command == "here" && Level >= 4)
                    {
                        Pet.PetData.AddExpirience(10);

                        Coord Coord = new Coord(-1,-1);

                        if (User.RotBody == 6)
                        {
                            Coord = new Coord(User.X - 1, User.Y);
                        }
                        else if (User.RotBody == 4)
                        {
                            Coord = new Coord(User.X, User.Y + 1);
                        }
                        else if (User.RotBody == 0)
                        {
                            Coord = new Coord(User.X, User.Y - 1);
                        }
                        else if (User.RotBody == 2)
                        {
                            Coord = new Coord(User.X + 1, User.Y);
                        }

                        if (Coord.x > -1 && Coord.y > -1)
                        {
                            Pet.MoveTo(Coord);
                        }

                        EnergyTimer = 10;
                        ActionTimer = 10;
                    }
                    else if (Command == "jump" && Level >= 10)
                    {
                        Pet.AddStatus("jmp", "");
                        Pet.PetData.AddExpirience(10);

                        EnergyTimer = 10;
                        ActionTimer = 10;
                    }
                    else
                    {
                        Pet.Chat(null, "*That command isnt included to me!*", false);
                    }

                    Pet.PetData.PetEnergy(false); // Remove Energy
                }
                else
                {
                    if (Pet.PetData.Energy <= 10)
                    {
                        RemovePetStatus();

                        if (GetBasketCoord().x > -1 && GetBasketCoord().y > -1 && Pet.Coordinate != GetBasketCoord())
                        {
                            Pet.MoveTo(GetBasketCoord());
                        }

                        Pet.AddStatus("lay", "");
                        Pet.Chat(null, "*Sleeps*", false);

                        SpeechTimer = 50;
                        ActionTimer = 45;
                        EnergyTimer = 5;
                    }
                    else
                    {
                        RemovePetStatus();
                        Pet.Chat(null, "*I'm not going to do that!*", false);

                        Pet.PetData.PetEnergy(false); // Remove Energy
                    }

                    Pet.UpdateNeeded = true;
                }
            }

            Pet = null;
        }
        #endregion

        public override void OnUserShout(Rooms.RoomUser User, string Message) { }

        public override void OnTimerTick()
        {
            #region Speech
            if (SpeechTimer <= 0)
            {
                RoomUser Pet = GetRoomUser();

                if (Pet != null)
                {
                    Random Random = new Random();
                    var Speeches = UberEnvironment.GetGame().GetBotManager().GetRandomForPetType(Pet.PetData.Type);

                    if (Speeches.Count <= 0) { return; } // no messages.

                    RemovePetStatus();

                    int SpeechIndex = Random.Next(0, Speeches.Count);

                    string GainedSpeech = Speeches[SpeechIndex]; if (GainedSpeech == "") { return; }

                    if (GainedSpeech.Length == 3 || (GainedSpeech.Split(' ')[0].Length == 3 && GainedSpeech.Split(' ')[1].Length == 3)) // Todo with action!
                    {
                        if (!Pet.Statusses.ContainsKey(GainedSpeech))
                        {
                            Pet.AddStatus(GainedSpeech, "");
                        }
                    }
                    else
                    {
                        Pet.Chat(null, GainedSpeech, false);
                    }

                    Pet = null;
                }

                SpeechTimer = UberEnvironment.GetRandomNumber(20, 120);
            }
            else
            {
                SpeechTimer--;
            }
            #endregion

            if (ActionTimer <= 0)
            {
                RemovePetStatus();

                RoomUser Pet = GetRoomUser();

                if (GetRoomUser().PetData.Energy <= 10)
                {
                    if (GetBasketCoord().x > -1 && GetBasketCoord().y > -1 && Pet.Coordinate != GetBasketCoord())
                    {
                        Pet.MoveTo(GetBasketCoord());
                    }

                    Pet.AddStatus("lay", "");
                    Pet.Chat(null, "*Sleeps*", false);
                }
                else
                {
                    int randomX = UberEnvironment.GetRandomNumber(0, GetRoom().Model.MapSizeX);
                    int randomY = UberEnvironment.GetRandomNumber(0, GetRoom().Model.MapSizeY);

                    if (randomX <= GetRoom().Model.MapSizeX && randomY <= GetRoom().Model.MapSizeY)
                    {
                        GetRoomUser().MoveTo(randomX, randomY);
                    }
                }

                ActionTimer = UberEnvironment.GetRandomNumber(15, 40 + GetRoomUser().PetData.VirtualId);
            }
            else
            {
                ActionTimer--;
            }

            if (EnergyTimer <= 0)
            {
                RemovePetStatus(); // Remove Status

                RoomUser Pet = GetRoomUser();

                if (Pet == null) { return; }

                Pet.PetData.PetEnergy(true); // Add Energy

                EnergyTimer = UberEnvironment.GetRandomNumber(30, 120); // 2 Min Max
            }
            else
            {
                EnergyTimer--;
            }
        }
    }
}